package com.hpli.domain.activity.service.quota.rule.impl;

import com.hpli.domain.activity.model.entity.ActivityCountEntity;
import com.hpli.domain.activity.model.entity.ActivityEntity;
import com.hpli.domain.activity.model.entity.ActivitySkuEntity;
import com.hpli.domain.activity.model.valobj.ActivityStateVO;
import com.hpli.domain.activity.service.quota.rule.AbstractActionChain;
import com.hpli.types.enums.ResponseCode;
import com.hpli.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @program: big-market-1-ting
 * @description: 活动规则过滤
 * @author: hpli
 * @create: 2025-04-12 19:19
 **/

@Slf4j
@Component("activity_base_action")
public class ActivityBaseActionChain extends AbstractActionChain {
    @Override
    public boolean action(ActivitySkuEntity activitySkuEntity, ActivityEntity activityEntity, ActivityCountEntity activityCountEntity) {
        log.info("活动责任链-基础信息【有效期、状态、库存（sku）】校验开始。 sku:{} activityId:{}",
                activitySkuEntity.getSku(), activityEntity.getActivityId());
        // 1.活动状态校验
        if (!ActivityStateVO.open.equals(activityEntity.getState())) {
            throw new AppException(ResponseCode.ACTIVITY_STATE_ERROR.getCode(),
                    ResponseCode.ACTIVITY_STATE_ERROR.getInfo());
        }

        // 2.活动时间校验
        Date currentTime = new Date();
        if (activityEntity.getStartDateTime().after(currentTime) || activityEntity.getEndDateTime().before(currentTime)) {
            throw new AppException(ResponseCode.ACTIVITY_DATE_ERROR.getCode(),
                    ResponseCode.ACTIVITY_DATE_ERROR.getInfo());
        }

        // 3.活动sku库存校验
        if (activitySkuEntity.getStockCountSurplus() <= 0) {
            throw new AppException(ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getCode(),
                    ResponseCode.ACTIVITY_SKU_STOCK_ERROR.getInfo());
        }

        return next().action(activitySkuEntity, activityEntity, activityCountEntity);
    }
}
